﻿using SetSailDal;
using SetSailModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SetSailCommon;

namespace SetSailView
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Frm_ZDGL : UserControl
    {
        public Frm_ZDGL()
        {
            InitializeComponent();
        }
        private List<QH_ZDMX> _zdmxList;
        private int pageIndex = 0;
        private void Frm_ZDGL_Load(object sender, EventArgs e)
        {
            this.init();
        }
        private void init()
        {
            //设置每页条目
            this.ucPagerControl21.PageSize = Convert.ToInt32(CookieData.getInstance().ZDGL.Where(a => a.MXDM == "PAGECOUNT").FirstOrDefault().MXZ);
            var totalCount = 0;
            this.LoadZDGLList(out totalCount);
            //设置分页
            this.setPage(totalCount);
        }
        /// <summary>
        /// 获取并绑定字典列表
        /// </summary>
        private void LoadZDGLList(out int totalCount, string where = "")
        {
            //获取字典管理
            ResultModel<List<QH_ZDGL>> result = ZDGLDal.GetZidianPageList(this.ucPagerControl21.PageIndex, this.ucPagerControl21.PageSize, where);
            if (result.Code == ResultCode.Success)
            {
                totalCount = result.Count;
                this.dgv_zdgl.AutoGenerateColumns = false;
                this.dgv_zdgl.DataSource = result.Data;
                //获取字典明细列表
                LoadZDMXList(result.Data.FirstOrDefault<QH_ZDGL>());
            }
            else
            {
                totalCount = 0;
                MessShow.ShowMistake(result.Message);
            }
        }

        private void setPage(int count)
        {
            List<object> lstPage2 = new List<object>();
            for (int i = 0; i < count; i++)
            {
                lstPage2.Add(i);
            }
            ucPagerControl21.DataSource = lstPage2;

            //ucPagerControl21.EndPage();
        }


        /// <summary>
        /// 字典明细列表
        /// </summary>
        /// <param name="_zdgl"></param>
        private void LoadZDMXList(QH_ZDGL _zdgl)
        {
            if (_zdgl != null)
            {
                ResultModel<List<QH_ZDMX>> result = ZDGLDal.GetZDMXList();
                if (result.Code == ResultCode.Success)
                {
                    _zdmxList = result.Data;
                    this.dgv_zdmx.AutoGenerateColumns = false;
                    this.dgv_zdmx.DataSource = _zdmxList.Where(o => o.ZDID == _zdgl.ID).ToList();
                }
                else
                {
                    MessShow.ShowMistake(result.Message);
                }
            }
        }

        /// <summary>
        /// 字典id 获取字典明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_zdgl_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = this.dgv_zdgl.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn15"].Value.ToString();
            this.dgv_zdmx.AutoGenerateColumns = false;
            this.dgv_zdmx.DataSource = _zdmxList.Where(o => o.ZDID == id).ToList();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ucBtnExt2_BtnClick(object sender, EventArgs e)
        {
            //筛选条件
            pageIndex = 0; //清空index，防止数据变更触发分页事件
            this.ucPagerControl21.FirstPage(); //查询后跳转第一条

            var totalCount = 0;
            this.LoadZDGLList(out totalCount, getwhere());
            //设置分页 查询后数据变更需要重新设置分页
            this.setPage(totalCount);
        }

        private string getwhere()
        {
            var zddm = txt_ZDDM.InputText;
            var where = string.Empty;
            if (zddm.Length > 0)
            {
                where += $" ZDDM like '%{zddm}%'";
            }
            return where;
        }

        /// <summary>
        /// 分页控件 数据操作事件
        /// </summary>
        /// <param name="currentSource"></param>
        private void ucPagerControl21_ShowSourceChanged(object currentSource)
        {
            if (currentSource != null)
            {
                var currindex = (Json.ToObject<int[]>(currentSource.ToJson()))[0];
                if (pageIndex != currindex)
                {
                    int count = 0;
                    LoadZDGLList(out count, getwhere());
                    pageIndex = currindex;
                }

            }
        }

        private void dgv_zdgl_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgv_zdgl.Columns[e.ColumnIndex].DataPropertyName == "btn")
            {
                DialogResult dialog = MessShow.ShowQuery("确实要删除记录吗？");
                if (dialog == DialogResult.OK)
                {

                    var result = ZDGLDal.EditZDGL(this.dgv_zdgl.Rows[e.RowIndex].Cells[0].Value.ToString());
                    if (result.Code == ResultCode.Success)
                    {
                        LoadZDGLList(out int count, getwhere());
                        MessShow.ShowSure("已删除记录");
                    }
                    else
                    {
                        MessShow.ShowHint(result.Message);
                    }
                }
            }
        }

        private void dgv_zdmx_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgv_zdmx.Columns[e.ColumnIndex].DataPropertyName == "btn")
            {
                DialogResult dialog = MessShow.ShowQuery("确实要删除记录吗？");
                if (dialog == DialogResult.OK)
                {
                    var zdid = this.dgv_zdgl.SelectedRows[0].Cells[0].Value.ToString();
                    var result = ZDGLDal.EditZDMX(this.dgv_zdmx.Rows[e.RowIndex].Cells[0].Value.ToString());
                    if (result.Code == ResultCode.Success)
                    {
                        SetSailModel.QH_ZDGL zdgl = new QH_ZDGL();
                        zdgl.ID = zdid;
                        LoadZDMXList(zdgl);
                        //LoadZDGLList(out int count, getwhere());
                        MessShow.ShowSure("已删除记录");
                    }
                    else
                    {
                        MessShow.ShowHint(result.Message);
                    }
                }
            }
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            Frm_ZDGLAdd add = new Frm_ZDGLAdd();
            add.IsMdiContainer = true;
            DialogResult result = add.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ucPagerControl21.FirstPage();
                LoadZDGLList(out int totalCount, getwhere());
                //LoadZDGLList(out int count, getwhere());
            }
        }

        private void ucBtnExt3_BtnClick(object sender, EventArgs e)
        {
            var ID = this.dgv_zdgl.SelectedRows[0].Cells[0].Value.ToString();
            if (ID.Length > 0)
            {
                Frm_ZDGLAdd add = new Frm_ZDGLAdd(ID);
                add.IsMdiContainer = true;
                DialogResult result = add.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.ucPagerControl21.FirstPage();
                    LoadZDGLList(out int totalCount, getwhere());
                }
            }
            else
            {
                MessShow.ShowSure("请选择需要修改的数据");
            }
        }
    }
}
